package com.hong.concurrent.chapter13;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author csh
 * @Description 生产者
 * @Date 2018/7/8 11:50
 */
public class ProducerThread extends Thread {

    private final MessageQueue messageQueue;

    private final static Random random = new Random(System.currentTimeMillis());

    private final static AtomicInteger counter = new AtomicInteger(0);

    public ProducerThread(MessageQueue messageQueue,int seq) {
        super("PRODUCER"+seq);
        this.messageQueue = messageQueue;
    }




    @Override
    public void run() {
        while (true){
            try {
                Message message = new Message("Message-" + counter.getAndIncrement());
                messageQueue.put(new Message("Message-"+counter.getAndIncrement()));
                System.out.println(Thread.currentThread().getName()+" put message");
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
